package com.zjxf.base.bean.sort;

import org.springframework.data.domain.Sort;

/**
 * Created with IntelliJ IDEA.
 *
 * @author zhanghao
 * date: 2018/8/10 16:05
 * description:
 */
public class SortUtils {

    /**
     * 构建排序
     *
     * @return Sort
     */
    public static Sort basicSort() {
        return basicSort(Sort.Direction.DESC.name(), "id");
    }

    /**
     * 构建排序
     *
     * @param orderType  排序类型
     * @param orderField 排序字段
     * @return Sort
     */
    public static Sort basicSort(String orderType, String orderField) {
        return new Sort(Sort.Direction.fromString(orderType), orderField);
    }

    /**
     * 构建多重排序
     *
     * @param sorts 多个排序组合
     * @return Sort
     */
    public static Sort basicSort(SortDto... sorts) {
        Sort result = null;
        for (SortDto dto : sorts) {
            if (result == null) {
                result = new Sort(Sort.Direction.fromString(dto.getOrderType()), dto.getOrderField());
            } else {
                result = result.and(new Sort(Sort.Direction.fromString(dto.getOrderType()), dto.getOrderField()));
            }
        }
        return result;
    }
}
